import java.util.*;

class Solution114_2 {
    public static void main(String[] args) {
        String[] str = {"wrt","wrf","er","ett","rftt"};
        System.out.println(alienOrder(str));
    }
    public static String alienOrder(String[] words) {
        if(words.length <= 1) return words[0] ;
        int len = words.length ;
        int[] rudu = new int[26] ;
        for(int i=0 ; i<26 ; i ++) rudu[i] = -1 ;
        Map<Character,Set<Character>> map = new HashMap<>() ;

        // 遍历数组 , 一个个字符串之间互相比较 ;
        for(int i=0 ; i < len ; i ++){
            for(int j=i+1 ; j < len ; j++){
                String a = words[i] , b = words[j] ;
                int z = 0 ;
                while(z < a.length() && z < b.length()){
                    char x = a.charAt(z) , y = b.charAt(z) ;
                    if(x != y){
                        if(map.containsKey(x)){
                            if(!map.get(x).contains(y)){
                                map.get(x).add(y) ;
                                if (rudu[y - 'a'] == -1) {
                                    rudu[y - 'a'] = 1;
                                } else {
                                    rudu[y - 'a']++;
                                }
                            }
                        }else{
                            map.put(x , new LinkedHashSet<Character>()) ;
                            map.get(x).add(y) ;
                            if (rudu[y - 'a'] == -1) {
                                rudu[y - 'a'] = 1;
                            } else {
                                rudu[y - 'a']++;
                            }
                        }
                        break ;
                    }else{
                        if(!map.containsKey(a.charAt(z))){
                            map.put(a.charAt(z) , new LinkedHashSet<Character>());
                            rudu[a.charAt(z) - 'a'] = 0 ;
                        }
                    }
                    z ++ ;

                }
                if(a.length() > b.length() && z == b.length() && a.charAt(z-1) == b.charAt(z-1)){
                    return "" ;
                }
                while(z < a.length() || z < b.length()){
                    if(z < a.length()){
                        if(!map.containsKey(a.charAt(z))){
                            map.put(a.charAt(z) , new LinkedHashSet<Character>());
                            rudu[a.charAt(z) - 'a'] = 0 ;
                        }
                    }
                    if(z < b.length()){
                        if(!map.containsKey(b.charAt(z))){
                            map.put(b.charAt(z) , new LinkedHashSet<Character>());
                            rudu[b.charAt(z) - 'a'] = 0 ;
                        }
                    }
                    z ++ ;
                }
            }
        }
        Queue<Character> queue =new LinkedList<>() ;
        StringBuilder strbu = new StringBuilder() ;
        for(int i=0 ; i < 26 ; i ++){
            if(rudu[i] == 0){
                queue.add((char)('a'+i)) ;
            }
        }
        while(!queue.isEmpty()){
            char ch = queue.poll() ;
            strbu.append(ch) ;
            Set<Character> set = map.get(ch) ;
            for(char ret : set){
                rudu[ret-'a'] -- ;
                if(rudu[ret-'a'] == 0){
                    queue.add(ret) ;
                }
            }
        }
        if(strbu.length() != map.size()){
            return "" ;
        }
        return strbu.toString() ;


    }
}